﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.Xml;
using System.IO;

namespace JKZP
{
    public partial class AddUserForm : Form
    {
        string defautpassword = "123456";
        public AddUserForm()
        {
            InitializeComponent();
        }

        private void AddUserForm_Load(object sender, EventArgs e)
        {
            try
            {
                label_newpassword.Text = defautpassword;
                XmlDocument doc = new XmlDocument();
                doc.Load(PP.gRoadFile);
                XmlNodeList nodes = doc.SelectNodes("descendant::road");
                if (nodes != null)
                { 
                    foreach(XmlNode node in nodes)
                    {
                        string roadid = node.Attributes["roadid"].Value;
                        string user = node.Attributes["user"].Value;
                        string roadname = node.Attributes["roadname"].Value;
                        checkedListBox1.Items.Add(roadid + "-" + roadname, false);
                    }
                }
            }
            catch { }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_username.Text == "")
                {
                    toolStripStatusLabel1.Text = "添加失败，用户名不能为空";
                    return;
                }

                DataGridView dgv = ((UserForm)(this.Owner)).dataGridView1;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    DataGridViewRow r = dgv.Rows[i];
                    if (textBox_username.Text == r.Cells["username"].Value.ToString())
                    {
                        toolStripStatusLabel1.Text = "添加失败，用户名已存在";
                        return;
                    }
                }

                string roadids = "";
                foreach(object o in checkedListBox1.CheckedItems)
                {
                    string roadid = o.ToString().Substring(0, o.ToString().IndexOf("-")+1);
                    roadids += roadid;
                }

                if (PP.kdws.JKZP_AddUser(textBox_username.Text, defautpassword, checkBox_admin.Checked.ToString(), roadids) == true)
                {
                    dgv.Rows.Add(new object[] { textBox_username.Text, checkBox_admin.Checked.ToString() });
                    toolStripStatusLabel1.Text = "添加成功";

                    PP.ReLoad();
                }
                else
                {
                    toolStripStatusLabel1.Text = "添加失败";
                }

                
            }
            catch { }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        
    }
}
